package com.example.light_fragment.activity;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

import com.example.light_fragment.R;
import com.example.light_fragment.R.id;
import com.example.light_fragment.R.layout;
import com.example.light_fragment.R.menu;
import com.example.light_fragment.db.DBManager;
import com.example.light_fragment.json.JsonUtils;
import com.example.light_fragment.service.ServiceHandler;

import android.R.anim;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.AnimRes;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ListView obj;
	private DBManager dbmanager;
	private String address;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		address = "http://10.0.0.2/user.json";
		ServiceHandler serviceClient = new ServiceHandler();
		String jsonData = serviceClient.makeServiceCall(address,
				ServiceHandler.GET);
		JsonUtils jsonUtils = new JsonUtils();
		jsonUtils.parseJson(jsonData);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user_id", "111"));
		params.add(new BasicNameValuePair("user_name", "ligo"));
		params.add(new BasicNameValuePair("password", "asdf3d"));
		params.add(new BasicNameValuePair("telephone", "13528123342"));
		params.add(new BasicNameValuePair("email", "472580488@qq.com"));
		serviceClient.makeServiceCall(address, ServiceHandler.POST, params);

		init_db();

		ArrayList array_list = dbmanager.getAllUserInfo();
		ArrayAdapter arrayAdapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1, array_list);
		obj = (ListView) findViewById(R.id.listView1);
		obj.setAdapter(arrayAdapter);

		obj.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "显示详细信息",
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	private void init_db() {
		// TODO Auto-generated method stub
		try {
			String packageName = this.getPackageName();
			InputStream inputStream = getAssets().open("light.db");
			dbmanager = new DBManager(packageName, inputStream);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
